<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="loginForm.username"
					type="text"
					maxlength="11"
					placeholder="用户名"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="loginForm.password"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			
			<!-- 其他登录 -->
			<!-- <view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view> -->
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="/pages/login/forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="/pages/login/register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import loginApi  from '@/request/api/loginApi.js'
	import userApi from '@/request/api/userApi.js'
	import userUtils from "@/utils/userUtils.js"
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				verCode:'',
				isRotate: false, //是否加载旋转
				isFocus: true, // 是否聚焦
				loginForm:{
					username:'', //用户/电话
					password:'', //密码
				}
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			//this.isLogin();
		},
		methods: {
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
		    async startLogin(e){
                console.log(e)
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.loginForm.username.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.loginForm.password.length < 4) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }	
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
				uni.showLoading({
					title: '登录中'
				});
				let loginData = await loginApi.passwordLogin(this.loginForm)
				if(loginData.code == 0 && loginData.token){
					userUtils.saveLoginInfo(loginData.token)	
					this.loginTip(true)
				}else{
					this.loginTip(false,loginData.msg)
				}
						
		    },
			//登录成功或失败提示
			loginTip(success,msg){
				if(success){
					uni.showToast({
						icon: 'success',
						position: 'bottom',
						title: '登录成功'
					}); 
					setTimeout(()=>{
						uni.reLaunch({
							url: '../index/index',
						});
					},1000)
				}else{
					_this.passData=""
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: msg
					});
				}
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
